const BASE = require('/utils/base')
const API = require('/utils/api')
App({
  onLaunch: function () { 
    //调用API从本地缓存中获取数据
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    var user = wx.getStorageSync('user');
    var openid = wx.getStorageSync('openid');
    if (!openid){
      BASE.login().then(data => {
        user = wx.getStorageSync('user');
        if (!user.mname){
          BASE.getMiniUser()
        }
      });
    }
    if (!user){
      API.getLoginUser(openid)
    }else{
      console.log('用户已登录, openid->', openid)
      console.log('user->', user)
    }
    
    // 跳转打车页面
    wx.redirectTo({
      url: '/pages/route/special',
    })
   
    // 获取系统状态栏信息
    wx.getSystemInfo({
      success: e => {
        this.globalData.StatusBar = e.statusBarHeight;
        this.globalData.CustomBar = e.platform == 'android' ? e.statusBarHeight + 50 : e.statusBarHeight + 45;
      }
    })
  }
  ,onShow: function () {
    this.globalData.user = API.getUser();
    this.globalData.userInfo = API.getUser();
  }
  , getUserInfo: function (cb) {
    var that = this
    console.log('getUserInfo-用户个人信息', that.globalData.userInfo)
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo
              console.log('用户个人信息', that.globalData.userInfo)
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      })
    }
  }
  //get locationInfo
  , getLocationInfo: function (cb) {
    var that = this;
    if (this.globalData.locationInfo) {
      cb(this.globalData.locationInfo)
    } else {
      wx.getLocation({
        type: 'gcj02', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
        success: function (res) {
          that.globalData.locationInfo = res;
          cb(that.globalData.locationInfo)
        },
        fail: function () {
          // fail
        },
        complete: function () {
          // complete
        }
      })
    }
  }

  , globalData: {
    userInfo: null
    , locationInfo: null
    , verifyCode: null
    , user: ''
    , StatusBar: ''
    , CustomBar: ''
  }
})